package com.project.class8_24.proxy.model1;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.time.LocalDate;
import java.time.LocalTime;

public class ExceptionProxy implements InvocationHandler {
    private Object targetObj;
    public ExceptionProxy(Object targetObj){
        this.targetObj = targetObj;
    }

    public Object getProxy(){
        return Proxy.newProxyInstance(targetObj.getClass().getClassLoader(),
                targetObj.getClass().getInterfaces(),this);
    }


    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object returnObj = null;

        try {
            //执行目标方法，得到方法执行后的返回值
            returnObj = method.invoke(targetObj, args);
        }catch (Exception e){
            String str = LocalDate.now()+"  "+ LocalTime.now()+"   执行";
            str += targetObj.getClass().getName()+"的"+method.getName()+"，抛出"
                    +e.getCause().getClass().getSimpleName();
            System.out.println(str);
        }

        return returnObj;
    }
}
